﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using _430GroupProject;
using System.Web.Security;
using System.Web.UI.HtmlControls;

namespace WebClient
{
    public partial class TeamManagement : System.Web.UI.Page
    {
        private ArrayList _data;
        private string SelectedTeam;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
            {
                Response.Redirect("~/NotAuthorized.aspx");
            }

        }


        protected void LoadNewData(object sender, EventArgs e)
        {

            ViewState["teamName"] = TeamSelect.SelectedValue;


            if (TeamSelect.SelectedValue.Equals("Create New") || TeamSelect.SelectedValue.Equals(""))
            {
                CreateTeamPanel.Visible = true;
                EditTeamPanel.Visible = false;
            }
            else
            {
                EditTeamPanel.Visible = true;
                CreateTeamPanel.Visible = false;
                GatherData(2);
                BindData();
            }
            //reateTeamFieldset.Visible = false;


        }

        protected void AddMemberToTeam(object sender, EventArgs e)
        {
            string newMemberNetID = NewMemberBox.Text;
        }

        protected void DeleteMember(object sender, DataGridCommandEventArgs e)
        {
            //object test = e.Item.;

            string toRemove = e.Item.Cells[0].Text;
            //remove netID from team in db
            //Gather Source

        }



        private void RemoveUser(object sender, DataGridItemEventArgs e)
        {
            //Use WCF to remove a User from a given team.  Delete User entirely if it is only a part of this team.
            GatherData(3);
            BindData();
        }

        private void GatherData(int testVal)
        {
            //String lookupName = Membership.GetUser().UserName;
            Users teamMembers = Master.Proxy.getUsersInWorkGroup(TeamSelect.SelectedValue);

            //Build list based on current User and Current Team.
            TeamMembersGrid.DataSource = teamMembers.Accounts;
            TeamMembersGrid.DataBind();

        }

        private void BindData()
        {
            //SupervisedUsers.DataSource = this._data;
            //SupervisedUsers.DataBind();
        }

        protected void CreateUserButton_Click(object sender, EventArgs e)
        {

        }

        protected void TeamSelect_Load(object sender, EventArgs e)
        {
            TeamSelect.EnableViewState = true;
            User user = (User)Session["user"];
            if (user != null)
            {
                List<string> toManage = new List<string>();
                if (user.Permissions.Contains(Permission.Admin))
                {
                    toManage = Master.Proxy.getAllWorkgroupNames();
                }
                else
                {
                    toManage = user.WorksIn;
                }
                toManage.Add("Create New");

                TeamSelect.DataSource = toManage;
                TeamSelect.DataBind();

                //Adjust page based on selected content
                if (TeamSelect.SelectedValue.Equals("Create New") || TeamSelect.SelectedValue.Equals(""))
                {
                    CreateTeamPanel.Visible = true;
                    EditTeamPanel.Visible = false;
                }
                else
                {
                    EditTeamPanel.Visible = true;
                    CreateTeamPanel.Visible = false;
                    GatherData(0);

                }
            }
            else
            {

            }
        }

        protected void CreateWorkGroup_Click(object sender, EventArgs e)
        {

            User payroll = Master.Proxy.getUser(WorkgroupPayroll.Text);
            User super = Master.Proxy.getUser(WorkgroupSupervisor.Text);
            if (payroll == null)
            {
                this.ErrorPanel.Visible = true;
                this.ErrorMessage.Text = "The user you have listed as payroll contact, " + WorkgroupPayroll.Text + "does not exist in the system.";
                this.MainPanel.Update();
                return;
            }

            if (super == null)
            {
                this.ErrorPanel.Visible = true;
                this.ErrorMessage.Text = "The user you have listed as supervisor, " + WorkgroupSupervisor.Text + "does not exist in the system.";
                this.MainPanel.Update();
                return;
            }

            if (payroll.WorksIn == null)
            {
                payroll.WorksIn = new List<string>();
            }
            payroll.WorksIn.Add(WorkgroupName.Text);
            Master.Proxy.updateUser(payroll);
            User user = (User)Session["user"];
            if (payroll.NetID.Equals(user.NetID))
            {
                Session["user"] = payroll;
            }

            if (super == null)
            {
                return;
            }

            if (super.WorksIn == null)
            {
                super.WorksIn = new List<string>();
            }
            super.WorksIn.Add(WorkgroupName.Text);
            Master.Proxy.updateUser(super);
            if (super.NetID.Equals(user.NetID))
            {
                Session["user"] = super;
            }
            WorkGroup toAdd = new WorkGroup(WorkgroupName.Text, payroll, super);
            toAdd.Schedules = new List<Schedule>();
            Master.Proxy.addWorkGroup(toAdd);
            MainPanel.Update();


        }

        protected void TeamMembersGrid_Load(object sender, EventArgs e)
        {
            User user = (User)Session["user"];
            if (user != null)
            {
                WorkGroups toManage = new WorkGroups();
                if (user.Permissions.Contains(Permission.Admin))
                {
                    toManage = Master.Proxy.getAllWorkgroups();
                }
                else
                {
                    toManage = Master.Proxy.SupervisorFor(user.NetID);
                }


                TeamMembersGrid.DataSource = toManage.Groups;
                TeamMembersGrid.DataBind();
                

                this.MainPanel.Update();



            }
            else
            {

            }
        }

        protected void AddMemberButton_Click(object sender, EventArgs e)
        {

            User user = Master.Proxy.getUser(NewMemberBox.Text);
            WorkGroup toUpdate = Master.Proxy.getWorkGroup(TeamSelect.SelectedValue);
            if (user != null)
            {
                if (user.WorksIn == null)
                {
                    user.WorksIn = new List<string>();
                }
                user.WorksIn.Add(TeamSelect.SelectedValue);
                toUpdate.addEmployee(user);
                if (newMemberSupervisor.Checked)
                {
                    if (user.Manages == null)
                    {
                        user.Manages = new List<string>();
                    }
                    user.Manages.Add(TeamSelect.SelectedValue);
                    toUpdate.SupervisorID = user.NetID;
                }
                Master.Proxy.updateUser(user);
                Master.Proxy.updateWorkGroup(toUpdate);
                MainPanel.Update();
                //Response.Redirect(Request.RawUrl);


            }
            else
            {
                this.ErrorPanel.Visible = true;
                this.ErrorMessage.Text = "The user you've attemted to add, " + NewMemberBox.Text + "does not exist in the system.";
                this.MainPanel.Update();
            }
        }

        protected void TeamMembersGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button clicked 
            // by the user from the Rows collection.
            GridViewRow row = TeamMembersGrid.Rows[index];
            if (e.CommandName.Equals("Delete"))
            {
                User toRemove = Master.Proxy.getUser(row.Cells[0].Text);
                toRemove.WorksIn.Remove(ViewState["teamName"] as string);
                toRemove.Manages.Remove(ViewState["teamName"] as string);
                Master.Proxy.updateUser(toRemove);
                //Now update Workgroup
                WorkGroup wg = Master.Proxy.getWorkGroup(ViewState["teamName"] as string);
                if (wg.SupervisorID.Equals(toRemove.NetID))
                {
                    wg.SupervisorID = "";
                }

                if (wg.PayrollID.Equals(toRemove.NetID))
                {
                    wg.PayrollID = "";
                }

                wg.removeEmployee(toRemove);
                Master.Proxy.updateWorkGroup(wg);

                this.MainPanel.Update();
                //Response.Redirect(Request.RawUrl);
            }
        }

        protected void ErrorClose_Click(object sender, EventArgs e)
        {
            ErrorPanel.Visible = false;
        }
    }
}